package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShopingCartMapper;
import com.sky.service.ShopingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: ShopingCartServiceImpl
 * Description: com.sky.service.impl
 */
@Service
public class ShopingCartServiceImpl implements ShopingCartService {

    @Autowired
    private ShopingCartMapper shopingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前加入到购物车的商品是否已经在购物车里
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 查询数据库
        List<ShoppingCart> list = shopingCartMapper.list(shoppingCart);

        // 如果已经存在了，数量加一就行
        if(list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);

            shopingCartMapper.updateNumber(shoppingCart1);
        }else{
            // 如果不存在，插入数据

            // 是否是菜品数据
            if(shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.getDishById1(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {

                Setmeal setmeal = setmealMapper.getSetmealById1(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopingCartMapper.insert(shoppingCart);
        }



    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> show() {

        Long userId = BaseContext.getCurrentId();

        List<ShoppingCart> list = shopingCartMapper.show(userId);

        return list;
    }

    /**
     * 清空购物车
     */
    public void clear() {

        Long userId = BaseContext.getCurrentId();
        shopingCartMapper.clear(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void clearOne(ShoppingCartDTO shoppingCartDTO) {

        Long userId = BaseContext.getCurrentId();

        if(shoppingCartDTO.getDishId() != null) {
            shopingCartMapper.clearOne(userId, shoppingCartDTO.getDishId(), null);
        }else {
            shopingCartMapper.clearOne(userId, null, shoppingCartDTO.getSetmealId());
        }


    }
}
